/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.comp.examples;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ontspace.MetadataRecordReference;
import org.ontspace.MetadataRepository;
import org.ontspace.owl.util.MetadataRepositoryConfigException;
import org.ontspace.comp.COMPQueryManager;
import org.ontspace.comp.owl.COMPQueryResultImpl;
import org.ontspace.lom.LOMQueryManager;
import org.ontspace.owl.MetadataRepositoryFactory;
import org.ontspace.owl.MetadataRepositoryImpl;
import org.ontspace.owl.util.OntologyNotInRepositoryException;

/**
 * Example for a comepetencies query
 */
public class ExampleCOMPQuerying {

    public static void main(String args[]) throws MetadataRepositoryConfigException,
            OntologyNotInRepositoryException {


        HashMap params =
                new HashMap();
        params.put(MetadataRepository.STORAGE_URI_PARAM,
                "jdbc:mysql://localhost/comp-app");
        params.put(MetadataRepository.USER_NAME_PARAM,
                "root");
        params.put(MetadataRepository.PWD_PARAM,
                "password");
        params.put(MetadataRepositoryImpl.DB_DRIVER_PARAM,
                "com.mysql.jdbc.Driver");
        params.put(MetadataRepositoryImpl.DB_TYPE_PARAM,
                "MySQL");
        params.put(MetadataRepositoryImpl.DC_SCHEMA_PATH_PARAM,
                "C:/mydirectory/cvs/ont-space/etc/competencies/clom.owl");

        try {

            params.put(MetadataRepository.PUBLIC_REP_URI_PARAM,
                    new URI("http://o-space.org/rep"));
        } catch (URISyntaxException ex) {
            Logger.getLogger(ExampleCOMPQuerying.class.getName()).log(Level.SEVERE,
                    null, ex);
        }



        MetadataRepository rep =
                MetadataRepositoryFactory.openMetadataRepository(params);


        HashMap paramsCOMP =
                new HashMap();
        paramsCOMP.put("uri1", "http://www.cc.uah.es/ie/ont/cLom-ospace");


        //Get the queryManager
        COMPQueryManager qm =
                (COMPQueryManager) rep.getQueryManager
                    ("org.ontspace.comp.owl.COMPQueryManagerImpl",paramsCOMP);



        COMPQueryResultImpl result =
                new COMPQueryResultImpl();
        String comp1 =
                "http://www.cc.uah.es/ie/ont/cLom-ospace#ComputerSystemEngineers";
        String comp2 =
                "http://www.cc.uah.es/ie/ont/cLom-ospace#individual_2";
        String comp3 =
                "http://www.cc.uah.es/ie/ont/cLom-ospace#individual_3";

        List<String> competList =
                new ArrayList<String>();
        competList.add(comp1);
        competList.add(comp2);
        competList.add(comp3);

        //get a query
        result =
                qm.searchByCompetency(competList);

        //LOM query manager for metadata queries
        HashMap paramsLOM =
                new HashMap();
        paramsLOM.put("uri1", "http://www.cc.uah.es/ie/ont/lom-ospace");

        LOMQueryManager lomQm =
                (LOMQueryManager) rep.getQueryManager
                    ("org.ontspace.lom.owl.LOMQueryManagerImpl",paramsLOM);


        for (Iterator iter =
                result.getMetadataRecordReference().iterator(); iter.hasNext();) {

            MetadataRecordReference mrr =
                    (MetadataRecordReference) iter.next();
            System.out.println();
            System.out.println("MetadataRecordReference - localId -> " +
                    mrr.getLocalMetadataRecordId());
            System.out.println("MetadataRecordReference - repositoryUri-> " +
                    mrr.getMRRAccessInterface().toString());



            //print metadata
            Set<String> titleResults =
                    lomQm.getLOMTitle(mrr.getLocalMetadataRecordId(), "");

            for (Iterator itTitle =
                    titleResults.iterator(); itTitle.hasNext();) {
                System.out.println("Title = " + itTitle.next().toString());
            }


            Set<String> descriptionResults =
                    lomQm.getLOMDescription(mrr.getLocalMetadataRecordId(), "");

            for (Iterator itDescription =
                    descriptionResults.iterator();
                    itDescription.hasNext();) {
                System.out.println("Description = " +
                        itDescription.next().toString());
            }

            Set<String> keywordResults =
                    lomQm.getLOMKeywords(mrr.getLocalMetadataRecordId(), "");

            for (Iterator itKeyword =
                    keywordResults.iterator();
                    itKeyword.hasNext();) {
                System.out.println("Keyword = " +
                        itKeyword.next().toString());
            }
        }

        List<String> comReq =
                qm.getRequiredCompetencies
                ("http://www.cc.uah.es/ie/ont/lom-ospace" +
                "#cLomLearningObjectComputerSystemEngineer");
        System.out.println("Required competencies: " + comReq);
    //show results

    }
}
